'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Public Class IcqContext
    Inherits Core.Service
    Implements Interfaces.IContext

    Private _Identity As IcqContact

    Public Sub New(ByVal uin As String)
        _Identity = New IcqContact(uin, uin)

        GetService(Of Interfaces.IConnector)()

        GetService(Of Interfaces.IStorageService)()
        GetService(Of Interfaces.IMessageService)()
        GetService(Of Interfaces.IIconService)()

        GetService(Of Interfaces.IDataWarehouseService)()
    End Sub

    'Private _Connector As IcqConnector
    'Public ReadOnly Property Connector() As IcqConnector
    '    Get
    '        Return _Connector
    '    End Get
    'End Property

    'Private _MessageService As IcqMessageService
    'Public ReadOnly Property MessageService() As IcqMessageService
    '    Get
    '        Return _MessageService
    '    End Get
    'End Property

    'Private _StorageService As IcqStorageService
    'Public ReadOnly Property StorageService() As IcqStorageService
    '    Get
    '        Return _StorageService
    '    End Get
    'End Property

    'Private _IconService As IconService
    'Public ReadOnly Property IconService() As IconService
    '    Get
    '        Return _IconService
    '    End Get
    'End Property

    'Private _DataWarehouse As IcqDataWarehouse
    'Public ReadOnly Property DataWarehouse() As IcqDataWarehouse
    '    Get
    '        Return _DataWarehouse
    '    End Get
    'End Property

    Private cachedBindings As New Dictionary(Of Type, Interfaces.IContextService)
    Private cachedInstances As New Dictionary(Of Type, Interfaces.IContextService)

    Public Function GetService(Of C As Interfaces.IContextService)() As C Implements Core.Interfaces.IServiceProvider(Of Interfaces.IContextService).GetService
        If Not cachedBindings.ContainsKey(GetType(C)) Then
            Dim type As Type = Core.Kernel.Mapper.GetImplementationType(Of C)()

            If Not cachedInstances.ContainsKey(type) Then
                cachedInstances.Add(type, DirectCast(Activator.CreateInstance(type, Me), C))
            End If

            cachedBindings.Add(GetType(C), cachedInstances(type))
        End If

        Return DirectCast(cachedBindings(GetType(C)), C)
    End Function

    Public ReadOnly Property Identity() As Interfaces.IContact Implements Interfaces.IContext.Identity
        Get
            Return _Identity
        End Get
    End Property

    Public Sub SetMyStatus(ByVal statusCode As Interfaces.IStatusCode) Implements Interfaces.IContext.SetMyStatus
        Dim dataOut As DataTypes.Snac011e
        Dim status As IcqInterface.DataTypes.UserStatus

        status = If(statusCode.Attributes("IcqUserStatus") Is Nothing, DataTypes.UserStatus.Offline, _
                    DirectCast(statusCode.Attributes("IcqUserStatus"), DataTypes.UserStatus))

        dataOut = New DataTypes.Snac011e
        dataOut.UserStatus.UserStatus = status

        Dim transfer As IIcqDataTranferService = DirectCast(GetService(Of Interfaces.IConnector)(), IIcqDataTranferService)
        transfer.Send(dataOut)
    End Sub
End Class
